//
//  WSJCrashTool.m
//  BaxTer
//
//  Created by wsj2015 on 2016/12/6.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "WSJCrashTool.h"

static WSJCrashTool *crashTool = nil;
@interface WSJCrashTool ()

@property (strong, nonatomic) KSCrashInstallation* crashInstallation;

@end

@implementation WSJCrashTool


+ (instancetype)shareCrashTool{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        crashTool = [WSJCrashTool new];
       
    });
    return crashTool;
}

- (void)config{
     self.crashInstallation = [self makeEmailInstallation];
     [self.crashInstallation install];
    [self.crashInstallation sendAllReportsWithCompletion:^(NSArray* reports, BOOL completed, NSError* error)
     {
         if(completed)
         {
             NSLog(@"Sent %d reports", (int)[reports count]);
         }
         else
         {
             NSLog(@"Failed to send reports: %@", error);
         }
     }];
    [KSCrash sharedInstance].deleteBehaviorAfterSendAll = YES;
}

- (KSCrashInstallation*) makeEmailInstallation
{
    NSString* emailAddress = @"598837443@qq.com";
    
    KSCrashInstallationEmail* email = [KSCrashInstallationEmail sharedInstance];
    email.recipients = @[emailAddress];
    email.subject = @"百乐学ios崩溃报告";
    email.message = @"百乐学ios崩溃报告详情..";
    email.filenameFmt = @"crash-report-%d.txt.gz";
    
    [email addConditionalAlertWithTitle:@"Crash Detected"
                                message:@"The app crashed last time it was launched. Send a crash report?"
                              yesAnswer:@"Sure!"
                               noAnswer:@"No thanks"];
    
    // Uncomment to send Apple style reports instead of JSON.
    [email setReportStyle:KSCrashEmailReportStyleApple useDefaultFilenameFormat:YES];
    
    return email;
}
- (void) configureAdvancedSettings
{
    KSCrash* handler = [KSCrash sharedInstance];
    
    // Settings in KSCrash.h
    handler.deadlockWatchdogInterval = 8;
    handler.userInfo = @{@"someKey": @"someValue"};
//    handler.onCrash = advanced_crash_callback;
//    handler.printTraceToStdout = YES;
    handler.handlingCrashTypes = KSCrashTypeUserReported;
    handler.searchThreadNames = YES;
    handler.searchQueueNames = YES;
    
    // Do not introspect class SensitiveInfo (see MainVC)
    // When added to the "do not introspect" list, the Objective-C introspector
    // will only record the class name, not its contents.
//    handler.doNotIntrospectClasses = @[@"SensitiveInfo"];
}
@end
